/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { isLiteralElement, isTagElement, isSelectElement, isArgumentElement, isDateElement, isTimeElement, isNumberElement, isPluralElement, TYPE, isPoundElement, } from './types.js';
export function printAST(ast) {
    return doPrintAST(ast, false);
}
export function doPrintAST(ast, isInPlural) {
    const printedNodes = ast.map(el => {
        if (isLiteralElement(el)) {
            return printLiteralElement(el, isInPlural);
        }
        if (isArgumentElement(el)) {
            return printArgumentElement(el);
        }
        if (isDateElement(el) || isTimeElement(el) || isNumberElement(el)) {
            return printSimpleFormatElement(el);
        }
        if (isPluralElement(el)) {
            return printPluralElement(el);
        }
        if (isSelectElement(el)) {
            return printSelectElement(el);
        }
        if (isPoundElement(el)) {
            return '#';
        }
        if (isTagElement(el)) {
            return printTagElement(el);
        }
    });
    return printedNodes.join('');
}
function printTagElement(el) {
    return `<${el.value}>${printAST(el.children)}</${el.value}>`;
}
function printEscapedMessage(message) {
    return message.replace(/([{}](?:.*[{}])?)/su, `'$1'`);
}
function printLiteralElement({ value }, isInPlural) {
    const escaped = printEscapedMessage(value);
    return isInPlural ? escaped.replace('#', "'#'") : escaped;
}
function printArgumentElement({ value }) {
    return `{${value}}`;
}
function printSimpleFormatElement(el) {
    return `{${el.value}, ${TYPE[el.type]}${el.style ? `, ${printArgumentStyle(el.style)}` : ''}}`;
}
function printNumberSkeletonToken(token) {
    const { stem, options } = token;
    return options.length === 0
        ? stem
        : `${stem}${options.map(o => `/${o}`).join('')}`;
}
function printArgumentStyle(style) {
    if (typeof style === 'string') {
        return printEscapedMessage(style);
    }
    else if (style.type === 1 /* SKELETON_TYPE.dateTime */) {
        return `::${printDateTimeSkeleton(style)}`;
    }
    else {
        return `::${style.tokens.map(printNumberSkeletonToken).join(' ')}`;
    }
}
export function printDateTimeSkeleton(style) {
    return style.pattern;
}
function printSelectElement(el) {
    const msg = [
        el.value,
        'select',
        Object.keys(el.options)
            .map(id => `${id} {${doPrintAST(el.options[id].value, false)}}`)
            .join(' '),
    ].join(', ');
    return `{${msg}}`;
}
function printPluralElement(el) {
    const type = el.pluralType === 'cardinal' ? 'plural' : 'selectordinal';
    const msg = [
        el.value,
        type,
        [
            el.offset ? `offset:${el.offset}` : '',
            ...Object.keys(el.options).map(id => `${id} {${doPrintAST(el.options[id].value, true)}}`),
        ]
            .filter(Boolean)
            .join(' '),
    ].join(', ');
    return `{${msg}}`;
}
